import fg from 'fast-glob'
import fs from 'fs'

const exec = async () => {
  const entries = await fg(['./src/components/*/*.tsx'])

  // 如果入口文件已经存在，则删除它，
  const arr = await fg(['./src/components/index.ts'])

  if (arr.length) {
    fs.unlinkSync('./src/components/index.ts')
  }

  // 入口文件里的代码用文件追加方式一句句生成
  for (let key in entries) {
    const fileFold = entries[key].split('/')[3]
    const fileName = entries[key].split('/')[4]
    fs.writeFile(
      './src/components/index.ts',
      `export { default as ${fileName.split('.')[0]} } from './${fileFold}/${fileName}'\n`,
      { flag: 'a' },
      function (err) {
        if (err) throw err
      }
    )
  }
}

exec()
